package com.xudow.app.dynamicstate_old.module.follow.viewholder;

import android.app.Activity;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.utils.JUtils;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.domain.Config;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.follow.DynamicDetailActivity;
import com.xudow.app.dynamicstate_old.module.follow.DynamicLongActivity;
import com.xudow.app.dynamicstate_old.module.follow.DynamicLongDetailActivity;
import com.xudow.app.dynamicstate_old.module.user.UserProfileActivity;
import com.xudow.app.dynamicstate_old.widget.ArticleSpan;
import com.xudow.app.dynamicstate_old.widget.exview.ExGridView;
import com.xudow.app.dynamicstate_old.widget.exview.NetImageAdapter;
import com.xudow.app.util.StringUtils;

import butterknife.BindView;

/**
 * Created by ske on 2016/5/22.
 */
public class RepostViewHolder extends DynamicContentViewHolder {
    // content
    @BindView(R.id.dynamic_follows_content_repost_content)
    TextView rePostContent;

    // 转发的普通内容
    @BindView(R.id.dynamic_follows_content_layout_repost_normal)
    LinearLayout normalRepostLayout; // 不需要时，隐藏此布局
    @BindView(R.id.dynamic_follows_content_repost_normal_author)
    TextView normalRepostAuthor;
    @BindView(R.id.dynamic_follows_content_repost_normal_tv)
    TextView normalRepostContent;
    @BindView(R.id.dynamic_follows_re_post_normal_img)
    ExGridView normalRepostImg;

    // 转发的长文内容，默认为gone
    @BindView(R.id.dynamic_follows_content_layout_repost_long)
    LinearLayout longRepostLayout; // 不需要时，隐藏此布局
    @BindView(R.id.dynamic_follows_content_repost_long_author)
    TextView longRepostAuthor;
    @BindView(R.id.dynamic_follows_content_repost_long_title)
    TextView longRepostTitle;
    @BindView(R.id.dynamic_follows_content_repost_long_tv)
    TextView longRepostContent;
    @BindView(R.id.dynamic_follows_content_repost_long_grid)
    ExGridView longRepostImgGridView;
    NetImageAdapter imageAdapter;

    public RepostViewHolder(RecyclerArrayAdapter<Dynamic> adapter, ViewGroup parent) {
        super(adapter, parent, R.layout.dynamic_follows_item_repost);
        normalRepostImg.setAdapter(imageAdapter = new NetImageAdapter(parent.getContext()));
        adapter.setNotifyOnChange(false);
    }

    @Override
    public void setData(Dynamic dynamic) {
        super.setData(dynamic);
        rePostContent.setText(StringUtils.isEmpty(dynamic.getForwardReason()) ? "转发动态" : dynamic.getForwardReason());

        //转发普通文章
        if (StringUtils.isEmpty(dynamic.getTitle())) {
            longRepostLayout.setVisibility(View.GONE);
            normalRepostLayout.setVisibility(View.VISIBLE);
            normalRepostAuthor.setText("@" + dynamic.getpUserName() + ":");
            // 设置content
            normalRepostContent.setText(dynamic.getContent());
            //图片
            if (!StringUtils.isEmpty(dynamic.getImgpath())) {
                imageAdapter.clear();
                String[] paths = dynamic.getImgpath().split(";");
                normalRepostImg.setVisibility(View.VISIBLE);
                normalRepostImg.setColumnCount(paths.length == 1 ? 2 : 3);
                imageAdapter.addAll(paths);
                imageAdapter.notifyDataSetChanged();
            } else {
                normalRepostImg.setVisibility(View.GONE);
            }
            normalRepostAuthor.setOnClickListener(v -> {
                Intent i = new Intent(getContext(), UserProfileActivity.class);
                i.putExtra(UserProfileActivity.USER_PROFILE_ID, dynamic.getpUserProfileId());
                ((Activity) getContext()).startActivityForResult(i, Config.Code_NeedRefresh);
            });

            normalRepostLayout.setOnClickListener(v -> {
                Intent i = new Intent(getContext(), DynamicDetailActivity.class);
                i.putExtra("id", dynamic.getpId());
                ((Activity) getContext()).startActivityForResult(i, Config.Code_NeedRefresh);
            });
        } else {
            longRepostLayout.setVisibility(View.VISIBLE);
            normalRepostLayout.setVisibility(View.GONE);
            longRepostAuthor.setText("@" + dynamic.getpUserName() + ":");
            longRepostTitle.setText(dynamic.getTitle());
            String source;
            if (dynamic.getContent().length() > 120)
                source = dynamic.getContent().substring(0, 120) + "..." + "全文详情";
            else
                source = dynamic.getContent() + "..." + "全文详情";
            SpannableString spannableString = new SpannableString(source);
            spannableString.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.name_color)), source.length() - 4, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new ArticleSpan(getContext()) {
                @Override
                public void onClick(View widget) {
                    Intent i = new Intent(getContext(), DynamicLongDetailActivity.class);
                    i.putExtra("dynamic", dynamic);
                    getContext().startActivity(i);
                }
            }, source.length() - 4, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            longRepostContent.setMovementMethod(LinkMovementMethod.getInstance());
            longRepostContent.setText(spannableString);

            if (!StringUtils.isEmpty(dynamic.getImgpath())) {
                imageAdapter.clear();
                String[] paths = dynamic.getImgpath().split(";");
                longRepostImgGridView.setVisibility(View.VISIBLE);
                longRepostImgGridView.setColumnCount(paths.length == 1 ? 2 : 3);
                imageAdapter.addAll(paths);
                imageAdapter.notifyDataSetChanged();
            } else {
                longRepostImgGridView.setVisibility(View.GONE);
            }
            longRepostAuthor.setOnClickListener(v -> {
                Intent i = new Intent(getContext(), UserProfileActivity.class);
                i.putExtra(UserProfileActivity.USER_PROFILE_ID, dynamic.getpUserProfileId());
                ((Activity) getContext()).startActivityForResult(i, Config.Code_NeedRefresh);
            });
            longRepostLayout.setOnClickListener(v -> {
                Intent i = new Intent(getContext(), DynamicLongActivity.class);
                i.putExtra("id", dynamic.getpId());
                ((Activity) getContext()).startActivityForResult(i, Config.Code_NeedRefresh);
            });
        }
    }
}
